
import pandas as pd
from ths.codeList import Tonghuashun
import baostock as bs
import talib
from util.GetBaostockDatByDataFrame import GetBaostockDataFrame
from util.WalkDataUtil import WalkDataUtil

if __name__ == "__main__":
    try:

        lg = bs.login(user_id='anonymous', password='123456')
        rs = bs.query_all_stock(day='2022-3-30')
        i=0
        df1 = []
        df =pd.DataFrame(columns=['high', 'open', 'close', 'low','ma25','bias_24','bizhi'])
        resdata = pd.DataFrame(columns=['ind', 'time', 'code'])
        while (rs.error_code == '0') & rs.next():
            try:
                print(i)
                i=i+1
                code = rs.get_row_data()[0]

                if "sh.60" in code or "sz.00" in code:



                    data = GetBaostockDataFrame().getData(bs, code, 'd', 220,191, False)

                    data['bias_24'] = (data['close'] - data['close'].rolling(24, min_periods=1).mean()) / data[
                        'close'].rolling(24, min_periods=1).mean() * 100
                    data['bias_24'] = round(data['bias_24'], 2)

                    walkData = WalkDataUtil().getWalkData(data)

                    d1=  walkData.iloc[0]
                    d2 = walkData.iloc[1]
                    d3 = walkData.iloc[2]
                    t1 =d1['type']
                    t2 = d2['type']
                    t3 = d3['type']
                    ind1 = d1['ind']
                    ind2 = d2['ind']
                    ind3 = d3['ind']
                    data1 = data.iloc[ind1]
                    data1_1 = data.iloc[ind1+1]
                    data2 = data.iloc[ind2]
                    data2_1 = data.iloc[ind2 + 1]
                    data3 = data.iloc[ind3]
                    data3_1 = data.iloc[ind3 + 1]



                    if  ind1-ind2 > 4 and  t1==2 and  t2==2  :
                        resdata = resdata.append({'code': code, 'ind':  ind1-ind2, 'time': d1['time']},ignore_index=True)

            except Exception as err:
                1+1
                #print(err)
        resdata = resdata.sort_values(by=['ind'], ascending=True)
        resdata = resdata[0:100]

        resdata = resdata.sort_values(by='time', ascending=True)
        resdata = resdata[0:20]

        print(resdata)

        print("运行完成!")



















        #t = Tonghuashun("test", "user_status=0; log=; Hm_lvt_f79b64788a4e377c608617fba4c736e2=1663469443; Hm_lvt_78c58f01938e4d85eaf619eae71b4ed1=1663426762,1663469443; cmsad_156_1=0; cmsad_152_1=0; cmsad_150_0=0; cmsad_160_1=0; user=MDq35kh0Njo6Tm9uZTo1MDA6MzI4ODA3NTY4OjcsMTExMTExMTExMTEsNDA7NDQsMTEsNDA7NiwxLDQwOzUsMSw0MDsxLDEwMSw0MDsyLDEsNDA7MywxLDQwOzUsMSw0MDs4LDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAxLDQwOzEwMiwxLDQwOjI0Ojo6MzE4ODA3NTY4OjE2NjM0Njk0Njc6OjoxNDUyODE0ODYwOjYwNDgwMDowOjE0NDc4OTcyZjg2NGE3ZWFiNDE2NzZkODJlNjYwZTIwNzpkZWZhdWx0XzQ6MQ%3D%3D; userid=318807568; u_name=%B7%E6Ht6; escapename=%25u950bHt6; ticket=2c44535375476af83bda4c7f04018e5c; utk=54cdad4f3d3fe57b3e86e436ba6a3c78; Hm_lpvt_78c58f01938e4d85eaf619eae71b4ed1=1663469470; cmsad_152_0=0; Hm_lpvt_f79b64788a4e377c608617fba4c736e2=1663469470; v=A5a90mIS6UfZI93jt9DZ1De650edN9pULHoO1QD_h3kUyDj5aMcqgfwLXurT")
        #data = t.get_stocks()
        #for index, row in data.iterrows():
        #    code = row['code']
            #t.modify_stock(code, 'del', pos='1')

        #添加
        #boo = t.modify_stock('834475', 'add', pos='1')
        #if boo :
        #    print('修改成功')


        #print(data)

    except Exception as err:
        print("Finished with error: ", err)